/**
 * Copyright 2005 SIB Framework

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.sibframework.utils;

public class SqlUtility {

	/**
	 * @param property java property name
	 * @return database column name.
	 */
	public static String property2Column(String property) {
		StringBuffer result = new StringBuffer();
		result.append(property.charAt(0));
		for (int i = 1; i < property.length(); i++) {
			if (property.charAt(i) <= 'Z' && property.charAt(i) >= 'A') {
				result.append("_");
			}
			result.append(Character.toLowerCase(property.charAt(i)));
		}
		
		return result.toString();
	}

	/**
	 * @param property java property getter method name
	 * @return database column name.
	 */
	public static String propertyGetterIser2Column(String property) {
		if (property.startsWith("is")) {
			return propertyMethod2Column(property, "is");
		} else if (property.startsWith("get")) {
			return propertyMethod2Column(property, "get");
		} else {
			return propertyMethod2Column(property, "");
		}
	}

	/**
	 * @param property java property setter name
	 * @return database column name.
	 */
	public static String propertySetter2Column(String property) {
		return propertyMethod2Column(property, "set");
	}
	
	/**
	 * @param property  java property access method name
	 * @return database column name.
	 */
	public static String propertyMethod2Column(String property, String pre) {
		if (property.startsWith(pre)) {
			String result = Character.toLowerCase(property.charAt(pre.length())) + property.substring(pre.length() + 1);
			return property2Column(result);
		}
		return null;
	}
	/**
	 * @param column database column name.
	 * @return String java property name
	 */
	public static String column2Property(String column) {
		String[] pices = column.split("_");
		StringBuffer result = new StringBuffer();
		if (pices.length < 1) {
			return null;
		}
		result.append(pices[0]);
		for (int i = 1; i < pices.length; i++) {
			if (pices[i].length() < 1) {
				continue;
			}
			result.append(pices[i].substring(0, 1).toUpperCase());
			if (pices[i].length() < 2) {
				continue;
			}
			result.append(pices[i].substring(1).toLowerCase());
		}
		return result.toString();
	}

	/**
	 * @param column database column name.
	 * @return   java property getter method name
	 */
	public static String column2PropertyGetter(String column) {
		return column2PropertyMethod(column, "get");
	}
	/**
	 * @param column database column name.
	 * @return  java property iser method name
	 */
	public static String column2PropertyIser(String column) {
		return column2PropertyMethod(column, "is");
	}
	/**
	 * @param column database column name.
	 * @return  java property setter method name
	 */
	public static String column2PropertySetter(String column) {
		return column2PropertyMethod(column, "set");
	}
	
	/**
	 * @param column database column name.
	 * @return  java property access method name.
	 */
	public static String column2PropertyMethod(String column, String pre) {
		String[] pices = column.split("_");
		StringBuffer result = new StringBuffer(pre);
		if (pices.length < 1) {
			return null;
		}
		for (int i = 0; i < pices.length; i++) {
			if (pices[i].length() < 1) {
				continue;
			}
			result.append(Character.toUpperCase(pices[i].charAt(0)));
			if (pices[i].length() < 2) {
				continue;
			}
			result.append(pices[i].substring(1).toLowerCase());
		}
		return result.toString();
	}
}
